#include "ULUI_LookupSingle.h"
#include "ULUI_LookupRange.h"
#include "ULUI_LookupLatest.h"
#include "ULUI_LookupLatestBlue.h"
#include "ULUI_UpdateHistory.h"
#include "ULUI_UpdateTxt.h"
#include "ULUI_UpdateBin.h"
#include "ULGfx_Statistic.h"
#include "UL_Global.h"

#define UPDATE_HISTORY_START_X	100
#define UPDATE_HISTORY_START_Y	100

ULUI_UpdateHistory ULUI_UpdateHistory::updateHistory;

enum HistoryLookupIDs{
	UPDATE_TXT,
	UPDATE_BIN,
	EXIT	
};	

static char *menuTitles[] = {
	"更新文本文件(history.txt)",
	"更新二进制文件(history.bin)",
	"返回"
};

void ULUI_UpdateHistory::Init()
{
	int i;
	ULUI_Menu menu;

	AttachEventHandler(this);

	menu.SetFont(UL_Global::UnionLottoFont());
	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}

	menus.SetFirstMenuXY(VSDL_Point(UPDATE_HISTORY_START_X, UPDATE_HISTORY_START_Y));

}

void ULUI_UpdateHistory::CleanUp()
{
	menus.Reset();
}

void ULUI_UpdateHistory::Pause()
{
}

void ULUI_UpdateHistory::Resume()
{
	selectedMenu = -1;
}

void ULUI_UpdateHistory::Update(VSDL_GameEngine *game)
{
	if (selectedMenu == -1){
		return ;
	}

	switch (selectedMenu){
		case UPDATE_TXT:
			game->PushState(ULUI_UpdateTxt::Instance());
			break;
		case UPDATE_BIN:
			game->PushState(ULUI_UpdateBin::Instance());
			break;
		case EXIT:
			game->PopState();
			break;
	}
}

void ULUI_UpdateHistory::Draw(VSDL_GameEngine *game)
{
	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(game->GetSurface());
}

ULUI_UpdateHistory *ULUI_UpdateHistory::Instance()
{
	return &updateHistory;
}


void __HANDLE_EVENT ULUI_UpdateHistory::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	if (keyStates[SDLK_ESCAPE]){
		game->PopState();
	}
}

void __HANDLE_EVENT ULUI_UpdateHistory::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	menus.UpdateMenuList(absX, absY);
}

void __HANDLE_EVENT ULUI_UpdateHistory::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	int i;
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;
		}
	}
	
}

void __HANDLE_EVENT ULUI_UpdateHistory::OnQuit(void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	game->Quit();
}
